-- UIShipMonsterInfo
-- Created by chengb Jun/9/2015
-- 幽灵船怪查看界面

require "game/ui/module/ship/ShipDisplay"

UIShipMonsterInfo = class("UIShipMonsterInfo", function()
    return cc.Layer:create();
end);

function UIShipMonsterInfo.create(ship)
    return UIShipMonsterInfo.new(ship);
end

local PROP_DESC_COUNT = 3;      -- 特性显示条数

-- 构造函数
function UIShipMonsterInfo:ctor(ship)
    self:setName("UIShipMonsterInfo");
    local node = cc.CSLoader:createNode("layout/monster/MonsterInfo.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 记录幽灵船对象
    self.ship = ship;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showTop();
            end
        elseif ev == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideTop();
            end
        end
    end);
end

-- 适配
function UIShipMonsterInfo:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册点击事件
function UIShipMonsterInfo:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIShipMonsterInfo");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDEL, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    local bg = findChildByName(self.node, "bg/bgImg");
    bg:setTouchEnabled(true);
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIShipMonsterInfo");
        end
    end
    bg:addTouchEventListener(onBgClick);
end

-- 重绘
function UIShipMonsterInfo:redraw()
    -- 怪物名称
    local classId = self.ship.classId;
    local titleLabel = findChildByName(self.node, "CT/bg/bg/title_label");
    titleLabel:setString(GhostShipM.query(classId, "name"));
    TextStyleM.setArtWordTitleStyle(titleLabel);

    local monsterNode = findChildByName(self.node, "CT/model_node");

    -- 怪物模型
    createShipModel(monsterNode, classId);

    monsterNode:setScale(1.2);

    -- 初始化属性
    self:initProp();

    -- 初始化特性
    self:initPropPanel();
end

-- 属性初始化
function UIShipMonsterInfo:initProp()
    local names = {"attack", "hp", "dodge", "accuracy"};
    local values = {
        self.ship:getAttack(),          -- 攻击
        math.max(self.ship:getHp(), 0), -- 生命
        0,                              -- 闪避（暂时写死）
        100,                            -- 命中（暂时写死）
    };

    local rightPart = findChildByName(self.node, "CT/bg/right_part");

    for i = 1, #names do
        local nameLabel = findChildByName(rightPart, names[i] .. "_node/name");
        TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
        nameLabel:setString(getLocStr(names[i]));

        local valueLabel = findChildByName(rightPart, names[i] .. "_node/value");
        TextStyleM.setTextStyle(valueLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
        valueLabel:setString(values[i]);
    end
end

-- 特性描述的panel初始化
function UIShipMonsterInfo:initPropPanel()
    --
    local scrollview = findChildByName(self.node, "CT/scrollview");
    scrollview:removeAllChildren();

    local itemNode = cc.CSLoader:createNode("layout/monster/MonsterInfoNode.csb");
    local widget = replaceNodeWithWidget(itemNode);

    -- 左边距
    local leftMargin = 1;

    -- 行间距
    local gap = 10;

    local startHeight = 10;

    -- 怪物描述
    local descInfo = GhostShipM.query(self.ship.classId, "monster_special_desc");

    local propBg;
    local propNameLabel;
    local propDescLabel;
    local totalHeight;
    local n = 1;
    local curHeight = 20;
    local ret = {};

    local itemName = "prop_";
    for i = 1, PROP_DESC_COUNT do

        if descInfo[i] ~= nil then
            -- 找到描述了，设置描述内容

            table.insert(ret, curHeight)

            local child = widget:clone();
            child:setName(itemName .. n);
            child:setPosition(255, curHeight);

            propNameLabel = findChildByName(child, "prop_name");
            propDescLabel = findChildByName(child, "prop_desc");
            propBg        = findChildByName(child, "prop_bg");

            -- 设置文本风格
            TextStyleM.setTextStyle(propNameLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
            TextStyleM.setTextStyle(propDescLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE);

            propNameLabel = applyString(propNameLabel, getMonsterLocStr(descInfo[i].name));
            propDescLabel = applyString(propDescLabel, getMonsterLocStr(descInfo[i].desc));

            -- 文本可见
            propNameLabel:setVisible(true);
            propDescLabel:setVisible(true);

            -- 获得高的值
            local nameHeight = propNameLabel:getContentSize().height;
            local descHeight = propDescLabel:getContentSize().height;

            local propHeight = nameHeight + descHeight + 36;
            propBg:setContentSize(propBg:getContentSize().width, propHeight);

            curHeight = curHeight + propHeight + 10;

            n = n + 1;

            scrollview:addChild(child);
        end
    end

    -- 重新调整位置
    --计算总高度
    local totalHeight = math.max(curHeight, scrollview:getInnerContainerSize().height);
    startHeight = totalHeight;

    scrollview:setInnerContainerSize(cc.size(scrollview:getInnerContainerSize().width, totalHeight));

    for j = 1, n - 1 do
        local child = findChildByName(scrollview, "prop_" .. j);
        local pos = ret[j];
        child:setPosition(cc.p(255, startHeight - pos));
    end
end

